package com.example.mall_system.service.Impl;

import com.example.mall_system.common.Result;
import com.example.mall_system.entity.CartItem;
import com.example.mall_system.entity.Product;
import com.example.mall_system.mapper.CartItemMapper;
import com.example.mall_system.service.CartItemService;
import com.example.mall_system.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CartItemServiceImpl implements CartItemService {

    @Autowired
    private CartItemMapper cartItemMapper;

    @Autowired
    private ProductService productService;

    @Override
    public Result<List<CartItem>> getCartItemsByUserId(Long userId) {
        List<CartItem> cartItems = cartItemMapper.findByUserId(userId);
        return Result.success(cartItems);
    }

    @Override
    public Result<String> addCartItem(Long userId, Long productId, Integer quantity) {
        // 验证商品是否存在
        @SuppressWarnings("unchecked")
        Result<Product> productResult = (Result<Product>) productService.getProductById(productId);
        if (productResult.getCode() != 200) {
            return Result.error(404, "商品不存在");
        }

        // 验证数量是否合法
        if (quantity <= 0) {
            return Result.error(400, "数量必须大于0");
        }

        // 检查购物车中是否已有该商品
        CartItem existingItem = cartItemMapper.findByUserIdAndProductId(userId, productId);
        if (existingItem != null) {
            // 更新数量
            existingItem.setQuantity(existingItem.getQuantity() + quantity);
            existingItem.setUpdateTime(new Date());
            cartItemMapper.updateQuantity(existingItem);
            return Result.success("购物车商品数量已更新");
        } else {
            // 添加新商品到购物车
            CartItem newItem = new CartItem();
            newItem.setUserId(userId);
            newItem.setProductId(productId);
            newItem.setQuantity(quantity);
            newItem.setCreateTime(new Date());
            newItem.setUpdateTime(new Date());
            cartItemMapper.insert(newItem);
            return Result.success("商品已添加到购物车");
        }
    }

    @Override
    public Result<String> updateCartItemQuantity(Integer id, Integer quantity, Long userId) {
        // 验证数量是否合法
        if (quantity <= 0) {
            return Result.error(400, "数量必须大于0");
        }

        // 验证购物车项是否存在且属于当前用户
        CartItem cartItem = cartItemMapper.findById(id);
        if (cartItem == null) {
            return Result.error(404, "购物车项不存在");
        }
        if (!cartItem.getUserId().equals(userId)) {
            return Result.error(403, "无权操作此购物车项");
        }

        // 更新数量
        cartItem.setQuantity(quantity);
        cartItem.setUpdateTime(new Date());
        cartItemMapper.updateQuantity(cartItem);
        return Result.success("购物车商品数量已更新");
    }

    @Override
    public Result<String> deleteCartItem(Integer id, Long userId) {
        // 验证购物车项是否存在且属于当前用户
        CartItem cartItem = cartItemMapper.findById(id);
        if (cartItem == null) {
            return Result.error(404, "购物车项不存在");
        }
        if (!cartItem.getUserId().equals(userId)) {
            return Result.error(403, "无权操作此购物车项");
        }

        // 删除购物车项
        cartItemMapper.deleteById(id);
        return Result.success("购物车商品已删除");
    }

    @Override
    public Result<String> clearCart(Long userId) {
        cartItemMapper.deleteByUserId(userId);
        return Result.success("购物车已清空");
    }
}